
var cloudwalkobj = document.createElement("object");
var CW_StrSeverIp = "120.25.161.56:7000";
var CW_StrHackServerIp = "";
var CW_appID = "user";
var CW_appSecret = "12345";
var CW_nLiveNum = "3";
var CW_nLiveLevel = "1";
var CW_nfaceValue = "0.7";
var CW_lience = "";
var CW_Varsion = "";
var CW_QualityScore = "0.75"
var CW_DstImageWidth = "319"
var CW_DstImageHeight = "425"
var CW_roix = "0"
var CW_roiy = "0"
var CW_roiWidth = "0"
var CW_roiHeight = "0"

//从页面上读取数据到内存
function ReadParam()
{
	var strSeverIp = document.getElementById("severIp").value;
	CW_StrSeverIp =  strSeverIp;
	
	var strHackServerIp = document.getElementById("hackServerIp").value;
	CW_StrHackServerIp = strHackServerIp;
	
	var strappid = document.getElementById("appid").value;
	CW_appID =  strappid;
	
	var strappsecret = document.getElementById("appsecret").value;
	CW_appSecret =  strappsecret;
	
	var strFaceValue = document.getElementById("faceValue").value;
	CW_nfaceValue = strFaceValue;
	
	var strLieveNum = document.getElementById("lieveNum").value;
	CW_nLiveNum = strLieveNum;
	
	
	var strLieveLevel = document.getElementById("lieveLevel").value;
	CW_nLiveLevel = strLieveLevel;
	
	var strLience = document.getElementById("License").value;
	CW_lience = strLience;
	
	var strVersion = document.getElementById("Version").value;
	CW_Varsion = strVersion;
	
	var strQuality = document.getElementById("qualitySCore").value;
	CW_QualityScore = strQuality;
	
	var strWidth = document.getElementById("dstImageWidth").value;
	CW_DstImageWidth = strWidth;
	
	var strHeight = document.getElementById("dstImageHeight").value;
	CW_DstImageHeight = strHeight;
	
	var strROIX = document.getElementById("roix").value;
	CW_roix = strROIX;
	
	var strROIY = document.getElementById("roiy").value;
	CW_roiy = strROIY;
	
	var strROIWidth = document.getElementById("roiWidth").value;
	CW_roiWidth = strROIWidth;
	
	var strROIHeight = document.getElementById("roiHeight").value;
	CW_roiHeight = strROIHeight;
	
	var bret = checkeValid();
	return bret;
}

//从 OCX 中读取配置，然后显示在界面上。
function WriteParam(configInfo)
{
	//alert(configInfo);
	var jsonObj = $.parseJSON(configInfo);
	if(jsonObj != "null")
	{
	 CW_StrSeverIp = jsonObj["ip"];
	 CW_StrHackServerIp = jsonObj["hackServerIp"];
	 CW_appID = jsonObj["appID"];
	 CW_appSecret = jsonObj["appSecret"];
	 CW_nfaceValue = jsonObj["faceValue"];
	 CW_nLiveNum = jsonObj["LiveDetectTimes"];
	 CW_nLiveLevel = jsonObj["LiveDetectLevel"];
	 CW_lience = jsonObj["License"];
	 CW_Varsion = jsonObj["Version"];
	 CW_QualityScore = jsonObj["qualityScoreThreshold"];
	 CW_DstImageWidth = jsonObj["dstFaceImageWidth"];
	 CW_DstImageHeight = jsonObj["dstFaceImageHeight"];
	 CW_roix = jsonObj["roix"];
	 CW_roiy = jsonObj["roiy"];
	 CW_roiWidth = jsonObj["roiWidth"];
	 CW_roiHeight = jsonObj["roiHeight"];
	 
	 document.getElementById("severIp").value = CW_StrSeverIp;
	 document.getElementById("hackServerIp").value = CW_StrHackServerIp;
	 document.getElementById("appid").value = CW_appID;
	 document.getElementById("appsecret").value = CW_appSecret;
	 document.getElementById("faceValue").value = CW_nfaceValue;
	 document.getElementById("lieveNum").value = CW_nLiveNum;
	 document.getElementById("lieveLevel").value = CW_nLiveLevel;
	 document.getElementById("License").value = CW_lience;
	 document.getElementById("Version").value = CW_Varsion;
	 document.getElementById("qualitySCore").value = CW_QualityScore;
	 document.getElementById("dstImageWidth").value = CW_DstImageWidth;
	 document.getElementById("dstImageHeight").value = CW_DstImageHeight;
	 document.getElementById("roix").value = CW_roix;
	 document.getElementById("roiy").value = CW_roiy;
	 document.getElementById("roiWidth").value = CW_roiWidth;
	 document.getElementById("roiHeight").value = CW_roiHeight;
	}
}


function createPlugin(targetE) {

	cloudwalkobj.width = 1;
	cloudwalkobj.height = 1;
	
	cloudwalkobj.id = "CloudWalkSDKPlugin";
	
	var bDet=BrowserDetect.browser;
	if("Explorer"==bDet)
	{
		cloudwalkobj.classid = "CLSID:B1597418-A51E-4140-8698-EE865439755C";	//IE
	}else if("Firefox"==bDet)
	{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";		//Firefox
	}else if("Chrome"==bDet)
	{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";		//Chrome
	}else{
		cloudwalkobj.type = "application/x-cloudface-sdk3.0";
	}

	targetE.appendChild(cloudwalkobj);
	
	if(!cloudwalkobj.valid){
		alert("插件未安装");
		return false;
	}
	
	var config = cloudwalkobj.cwGetConfig();
	WriteParam(config);
}

function checkeValid()
{
	if(CW_StrSeverIp.length == 0)
	{
		alert("服务ip不能为空");
		return false;
	}
	var arg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\:[0-9]+$/;
	if(CW_StrSeverIp.match(arg) == null)
	{
		alert("服务ip端口格式不正确");
		return false;
	}
	
	if(CW_appID.length == 0)
	{
		alert("appID不能为空");
		return false;
	}
	if(CW_appSecret.length == 0)
	{
		alert("appSecret不能为空");
		return false;
	}
	
	if(CW_nfaceValue.length == 0)
	{
		alert("人脸阈值不能为空");
		return false;
	}
	else
	{
		var fTemp = parseFloat(CW_nfaceValue);
		if(isNaN(fTemp))
		{
			alert("人脸阈值请输入0~1之间的数字");
			return false;
		}
		if(fTemp < 0 || fTemp >1)
		{
			alert("人脸阈值请输入0~1之间的数字");
			return false;
		}
	}
	
	if(CW_QualityScore.length == 0)
	{
		alert("质量阈值不能为空");
		return false;
	}
	else
	{
		var fTemp = parseFloat(CW_QualityScore);
		if(isNaN(fTemp))
		{
			alert("质量阈值请输入0~1之间的数字");
			return false;
		}
		if(fTemp < 0 || fTemp >1)
		{
			alert("质量阈值请输入0~1之间的数字");
			return false;
		}
	}
	
	if(CW_nLiveNum.length == 0)
	{
		alert("活体个数不能为空");
		return false;
	}
	else
	{
		var fTemp = parseFloat(CW_nLiveNum)
		var nTemp = parseInt(CW_nLiveNum);
		if( isNaN(nTemp) || (fTemp - nTemp > 0.00001) || nTemp < 0 || nTemp > 6)
		{
			alert("活体个数请输入 0 ~ 6 之间的整数");
			return false;
		}
	}
	
	if(CW_nLiveLevel.length == 0)
	{
		alert("活体等级不能为空");
		return false;
	}
	else
	{
		var fTemp = parseFloat(CW_nLiveLevel)
		var nTemp = parseInt(CW_nLiveLevel);
		if( isNaN(nTemp) || (fTemp - nTemp > 0.00001) || nTemp < 1 || nTemp > 3)
		{
			alert("活体等级请输入1~3之间的整数");
			return false;
		}
	}
	
	if(CW_lience.length == 0)
	{
		alert("授权码不能为空");
		return false;
	}
	
	if(CW_DstImageWidth.length == 0)
	{
		alert("输出人脸宽不能为空");
		return false;
	}
	else
	{
		if(Math.floor(CW_DstImageWidth) != CW_DstImageWidth)
		{
			alert("输出人脸宽请输入数字");
			return false;
		}
	}
	
	if(CW_DstImageHeight.length == 0)
	{
		alert("输出人脸高不能为空");
		return false;
	}
	else
	{
		if(Math.floor(CW_DstImageHeight) != CW_DstImageHeight)
		{
			alert("输出人脸高请输入数字");
			return false;
		}
	}
	
	if(CW_roix < 0 || CW_roiy < 0 || CW_roiWidth < 0 || CW_roiHeight < 0)
	{
		alert("ROI设置错误");
		return false;
	}
	
	/*if(CW_Varsion.length == 0)
	{
		alert("版本不能为空");
		return false;
	}*/
	
	return true;
}

function Init()
{
	var el = document.getElementById("cloudwalkwebobj");
	createPlugin(el); 
}

function Save()
{
	var bRet = ReadParam();
	if(!bRet)
	{
		return;
	}
	
	var jsInfo = new Object();
	jsInfo.ip = CW_StrSeverIp;
	jsInfo.hackServerIp = CW_StrHackServerIp;
	jsInfo.appID = CW_appID;
	jsInfo.appSecret = CW_appSecret;
	jsInfo.LiveDetectTimes = CW_nLiveNum;
	jsInfo.LiveDetectLevel = CW_nLiveLevel;
	jsInfo.faceValue = CW_nfaceValue;
	jsInfo.License = CW_lience;
	jsInfo.Version = CW_Varsion;
	jsInfo.qualityScoreThreshold = CW_QualityScore;
	jsInfo.dstFaceImageWidth = CW_DstImageWidth;
	jsInfo.dstFaceImageHeight = CW_DstImageHeight;
	jsInfo.roix = CW_roix;
	jsInfo.roiy = CW_roiy;
	jsInfo.roiWidth = CW_roiWidth;
	jsInfo.roiHeight = CW_roiHeight;

	var last=JSON.stringify(jsInfo);
	var nRet = cloudwalkobj.cwSetConfig(last);
	if(!nRet)
	{
		alert("保存成功");
	}
	else
	{
		alert("保存失败");
	}
}

//显示关于对话框
function ShowAboutBox()
{
	cloudwalkobj.AboutBox();
}